package com.bytedance.scenedemo.activity_compatibility.activity_result;

import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scene.ui.template.AppCompatScene;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.util.ImageUtil;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.media.image.PixelMap;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

/**
 * Scene拿Ability返回值
 *
 * @author JiangQi
 * @since 2018-03-08
 */
public class SceneGetAbilityResultSample extends AppCompatScene {
    @Override
    protected Component onCreateContentView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        Component parse = inflater.parse(ResourceTable.Layout_nav_scene_result_layout, container, false);
        Text text = (Text) parse.findComponentById(ResourceTable.Id_text_navigation_list_hint);
        text.setText(getClassName());
        initListener(parse);
        setTitle("Scene拿Ability返回值");
        return parse;
    }

    private void initListener(Component parse) {
        Button btn2 = (Button) parse.findComponentById(ResourceTable.Id_btn2);
        Image image = (Image) parse.findComponentById(ResourceTable.Id_image);
        btn2.setClickedListener(component -> {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withAction(IntentConstants.ACTION_CHOOSE)
                    .build();
            intent.setType("image/*");
            intent.setOperation(operation);
            SceneExtensions.requireNavigationScene(this).startActivityForResult(intent, 12345, (resultCode, result) -> {
                if (resultCode == -1) {
                    if (result.getUriString() != null) {
                        Uri uri = result.getUri();
                        String uriString = result.getUriString();
                        PixelMap pixelMap = ImageUtil.getPixelMap2(getApplicationContext(), uriString);
                        image.setPixelMap(pixelMap);
                    }
                }
            });
        });

        Button btn3 = (Button) parse.findComponentById(ResourceTable.Id_btn3);
        btn3.setClickedListener(component -> {
            showToast("回调");
        });
    }
}
